/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import controller.CatererJpaController;
import controller.RestaurantJpaController;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.http.HttpServletRequest;
import model.entity.Caterer;
import model.entity.Restaurant;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author lnngu_000
 */
@ManagedBean
public class RestaurantBean extends ActionSupport implements
        ServletRequestAware {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("OnlineCateringPU");
    private int restaurantId;
    private int catererId;
    private Boolean isVegetarian;
    private Boolean isNonVeg;
    private String regional;
    private String provinces;
    private String location;
    private String numberOfPlates;
    private int experience;
    private Boolean isActive;
    private String descriptions;
    private String dish;
    private String openTime;
    private String closeTime;
    private String imageUrl;
    private String name;
    private String address;
    private String typeOfMenu;

    public int getRestaurantId() {
        return restaurantId;
    }

    public String getTypeOfMenu() {
        return typeOfMenu;
    }

    public void setTypeOfMenu(String typeOfMenu) {
        this.typeOfMenu = typeOfMenu;
    }

    public void setRestaurantId(int restaurantId) {
        this.restaurantId = restaurantId;
    }

    public int getCatererId() {
        return catererId;
    }

    public void setCatererId(int catererId) {
        this.catererId = catererId;
    }

    public Boolean getIsVegetarian() {
        return isVegetarian;
    }

    public void setIsVegetarian(Boolean isVegetarian) {
        this.isVegetarian = isVegetarian;
    }

    public Boolean getIsNonVeg() {
        return isNonVeg;
    }

    public void setIsNonVeg(Boolean isNonVeg) {
        this.isNonVeg = isNonVeg;
    }

    public String getRegional() {
        return regional;
    }

    public void setRegional(String regional) {
        this.regional = regional;
    }

    public String getProvinces() {
        return provinces;
    }

    public void setProvinces(String provinces) {
        this.provinces = provinces;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getNumberOfPlates() {
        return numberOfPlates;
    }

    public void setNumberOfPlates(String numberOfPlates) {
        this.numberOfPlates = numberOfPlates;
    }

    public int getExperience() {
        return experience;
    }

    public void setExperience(int experience) {
        this.experience = experience;
    }

    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        this.isActive = isActive;
    }

    public String getDescriptions() {
        return descriptions;
    }

    public void setDescriptions(String descriptions) {
        this.descriptions = descriptions;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public String getDish() {
        return dish;
    }

    public void setDish(String dish) {
        this.dish = dish;
    }

    public String getOpenTime() {
        return openTime;
    }

    public void setOpenTime(String openTime) {
        this.openTime = openTime;
    }

    public String getCloseTime() {
        return closeTime;
    }

    public void setCloseTime(String closeTime) {
        this.closeTime = closeTime;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
    private HttpServletRequest servletRequest;
    private File images;
    private String imagesContentType;
    private String imagesFileName;

    public File getImages() {
        return images;
    }

    public void setImages(File images) {
        this.images = images;
    }

    public String getImagesContentType() {
        return imagesContentType;
    }

    public void setImagesContentType(String imagesContentType) {
        this.imagesContentType = imagesContentType;
    }

    public String getImagesFileName() {
        return imagesFileName;
    }

    public void setImagesFileName(String imagesFileName) {
        this.imagesFileName = imagesFileName;
    }
    RestaurantJpaController restaurantControl = new RestaurantJpaController(emf);

    public String add() {
        try {

            String filePath = servletRequest.getSession().getServletContext().getRealPath("customer/");
            filePath = filePath.replace("\\build\\web\\customer", "\\web\\images\\customer");
            File fileToCreate = new File(filePath, this.getImagesFileName());

            Restaurant restaurant = new Restaurant();
            CatererJpaController catererControl = new CatererJpaController(emf);
            restaurant.setAddress(this.getAddress());
            restaurant.setCatererId(catererControl.findCaterer(3));
            restaurant.setCloseTime(this.getCloseTime());
            restaurant.setDescriptions(this.getDescriptions());
            restaurant.setDish(this.getDish());
            restaurant.setExperience(this.getExperience());
            restaurant.setImageUrl(this.getImagesFileName());
            if (this.getTypeOfMenu().equals("Vegetarian")) {
                restaurant.setIsVegetarian(true);
                restaurant.setIsNonVeg(false);
            }
            if (this.getTypeOfMenu().equals("Non-Veg")) {
                restaurant.setIsVegetarian(false);
                restaurant.setIsNonVeg(true);
            }
            restaurant.setIsActive(true);
            restaurant.setLocation(this.getLocation());
            restaurant.setName(this.getName());
            restaurant.setNumberOfPlates(this.getNumberOfPlates());
            restaurant.setOpenTime(this.getOpenTime());
            restaurant.setProvinces(this.getProvinces());
            restaurant.setRegional(this.getRegional());
            restaurantControl.create(restaurant);
            FileUtils.copyFile(this.images, fileToCreate);
            addActionMessage("The add new restaurant successfull. ");
            emf.close();
            return "success";
        } catch (Exception e) {
            addActionError("The add new restaurant fail. ");
            return "fail";
        }

    }

    public String getAllRestaurants() {
        try {
            Map session = ActionContext.getContext().getSession();
            List ls = restaurantControl.findRestaurantEntities();
            session.put("AllRestaurants", restaurantControl.findRestaurantEntities());
            emf.close();
            return "success";
        } catch (Exception e) {
            return "fail";
        }
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.servletRequest = hsr;
    }

    @Override
    public void validate() {
        String actionName = ActionContext.getContext().getName();
        if (actionName.equals("addRestaurant")) {
            if (this.getName().equals("")) {
                addActionError("Restaurant Name is required.");
            }
            if (this.getAddress().equals("")) {
                addActionError("Address is required.");
            }
            if (this.getDish().equals("")) {
                addActionError("Dish is required.");
            }
        }
    }
}
